package com.yao.seckill.web.listener.rabbitmq;

import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.yao.seckill.web.vo.RabbitVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.stereotype.Component;

import javax.annotation.processing.Messager;
import java.io.IOException;

/**
 * Copyright (C), 2022-2022, 姚兵
 * Author: 32210
 * Date: 2022/9/16 12:54
 * FileName: DemoListener
 * Description: rabbit
 */

@Component

@Slf4j
public class DemoListener {

    @RabbitListener(queues = {"#{T(com.yao.seckill.web.constant.RabbitConstant.DemoQueue).DEMO_QUEUE}"})
    public void handle(RabbitVo rabbitVo, Message message,Channel channel){
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            log.info("receive msg={}", JSONObject.toJSONString(rabbitVo));

            channel.basicAck(deliveryTag,false);
        }catch (Exception e){
            log.info("exception e={}",e);
            try {
                channel.basicReject(deliveryTag,true);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }

    }
}
